package com.dwjm.shop.utils;

import com.dwjm.shop.goods.Goods;
import com.dwjm.shop.goods.GoodsDao;
import com.dwjm.shop.goods.GoodsDaoImpl;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@WebServlet({"/getGoodsById", "/updateGoods"})
public class EditGoodsServlet extends HttpServlet {
    private GoodsDao goodsDao = new GoodsDaoImpl();
    private Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        String idStr = request.getParameter("id");
        if (idStr != null) {
            try {
                int id = Integer.parseInt(idStr);
                Goods goods = goodsDao.getGoodsById(id);
                Map<String, Object> result = new HashMap<>();
                if (goods != null) {
                    result.put("code", 200);
                    result.put("data", goods);
                } else {
                    result.put("code", 404);
                    result.put("msg", "未找到该商品");
                }
                response.getWriter().write(gson.toJson(result));
            } catch (NumberFormatException e) {
                Map<String, Object> result = new HashMap<>();
                result.put("code", 400);
                result.put("msg", "商品 ID 格式错误");
                response.getWriter().write(gson.toJson(result));
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        String idStr = request.getParameter("id");
        String title = request.getParameter("title");
        String priceStr = request.getParameter("price");
        String descr = request.getParameter("descr");
        String img = request.getParameter("img");

        try {
            int id = Integer.parseInt(idStr);
            BigDecimal price = new BigDecimal(priceStr);

            Goods goods = new Goods();
            goods.setId(id);
            goods.setTitle(title);
            goods.setPrice(price);
            goods.setDescr(descr);
            goods.setImg(img);

            int result = goodsDao.updateGoods(goods);
            Map<String, Object> res = new HashMap<>();
            if (result == 1) {
                res.put("code", 200);
                res.put("msg", "商品信息更新成功");
            } else {
                res.put("code", 500);
                res.put("msg", "商品信息更新失败");
            }
            response.getWriter().write(gson.toJson(res));
        } catch (NumberFormatException e) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 400);
            result.put("msg", "商品 ID 或价格格式错误");
            response.getWriter().write(gson.toJson(result));
        }
    }
}